const assert = require("assert");
const { createLogger } = require("./shared");
const { runner } = require("./shared");
const path = require("path");
const spawn = require("cross-spawn");

const logger = createLogger("start.js");

// 当前正在运行的 脚本名称
const currentScript = process.env.npm_lifecycle_event;

const scriptFlag = "start:";

assert.notStrictEqual(
  currentScript,
  undefined,
  "必须使用 npm 或者 yarn 来启动脚本"
);

assert.strictEqual(
  currentScript.startsWith(scriptFlag),
  true,
  `script 配置 请使用 ${scriptFlag} 开头`
);

const packageName = currentScript.replace(scriptFlag, "");

assert.notStrictEqual(packageName, "", "必须输入 package 名称");

logger({ currentScript, packageName });

spawn(runner, ["start"], {
  stdio: "inherit",
  cwd: path.resolve(process.cwd(), `packages/${packageName}`),
});
